/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.helper;

import com.ivanbiz.model.ItemNota;
import com.ivanbiz.model.ItemPembelian;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author User
 */
public class EngineReport {

    PrintWriter pw = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    NumberFormat numberFormat = NumberFormat.getInstance(Locale.US);
    String tujuan = "E:/trunk/BergunaJaya/report/Laporan.txt";
    String openFile = "cmd /c start E:/trunk/BergunaJaya/report/Laporan.txt";
    String path = "D:\\BergunaJaya\\BackUp\\bergunajaya_";
    String tujuanPath = "C:\\Windows\\dump.bat";
    String backup = "cmd /c start C:\\Windows\\dump.bat";

    public StringBuffer createField(int sizeField, String data) {
        String[] fieldSize = new String[sizeField];
        char[] isi = data.toString().toCharArray();
        StringBuffer buff = new StringBuffer();
        if (fieldSize.length < isi.length) {
            for (int x = 0; x < fieldSize.length; x++) {
                fieldSize[x] = String.valueOf(isi[x]);
                buff.append(fieldSize[x]);
            }
        } else {
            for (int x = 0; x < isi.length; x++) {
                fieldSize[x] = String.valueOf(isi[x]);
                buff.append(fieldSize[x]);
            }
        }

        for (int x = 0; x < fieldSize.length; x++) {
            if (fieldSize[x] == null) {
                buff.append(" ");
            }
        }
        return buff;
    }

    public void backUpData() {
        try {
            StringBuffer cetak = new StringBuffer();
            cetak.append("mysqldump -u root -p admin bergunajaya > " + path + sdf.format(new Date()) + ".sql \n");
            cetak.append("exit");
            pw = new PrintWriter(tujuanPath);
            pw.write(cetak.toString());
            Runtime.getRuntime().exec(backup);
        } catch (IOException ex) {
            Logger.getLogger(EngineReport.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            pw.flush();
            pw.close();
        }
    }

    public void printReportPenjualan(List listItemNota, Date startDate, Date endDate) {
//        PrintWriter pw = null;
        try {
            StringBuffer cetak = new StringBuffer();
            StringBuffer header = new StringBuffer();
            double total = 0;
            cetak.append("PT BERGUNA JAYA\n");
            cetak.append("Jln. Jembatan Selatan no 470 Panglima Polim\n");
            cetak.append("Telp. 7250923 Fax. 7234053\n");
            cetak.append("Kebayoran Baru - Jakarta Selatan\n");
            cetak.append("                                                           Laporan Penjualan : " + sdf.format(startDate) + " s/d " + sdf.format(endDate) + "\n");
            cetak.append("\n");
            header.append(createField(5, "| NO "));
            header.append(createField(11, "| TANGGAL  "));
            header.append(createField(15, "|     NOTA     "));
            header.append(createField(7, "| T/K  "));
            header.append(createField(10, "| KD.CUST "));
            header.append(createField(16, "| NAMA CUSTOMER "));
//            header.append(createField(14, "|    TELP     "));
            header.append(createField(25, "|      NAMA BARANG       "));
            header.append(createField(16, "|      HARGA     "));
            header.append(createField(7, "|JUMLAH"));
            header.append(createField(19, "|     SUBTOTAL    |"));
            cetak.append(header + "\n");
            String kode = "";
            int no = 0;
            for (int x = 0; x < listItemNota.size(); x++) {
                ItemNota itemNota = (ItemNota) listItemNota.get(x);
                if (!itemNota.getNota().getKode().equals(kode)) {
                    kode = itemNota.getNota().getKode();
                    StringBuffer nomor = createField(5, "|" + (no += 1));
                    cetak.append(nomor);
                    StringBuffer tanggalNota = createField(11, "|" + sdf.format(itemNota.getNota().getCreateDate()));
                    cetak.append(tanggalNota);
                    StringBuffer noNota = createField(15, "|" + itemNota.getNota().getKode());
                    cetak.append(noNota);
                    StringBuffer statusPemabayaran = createField(7, "|" + itemNota.getNota().getStatusPembayaran());
                    cetak.append(statusPemabayaran);
                    StringBuffer kodeCustomer = createField(10, "|" + itemNota.getNota().getCustomer().getKode());
                    cetak.append(kodeCustomer);
                    StringBuffer namaCustomer = createField(16, "|" + itemNota.getNota().getCustomer().getNama());
                    cetak.append(namaCustomer);
                } else {
                    StringBuffer nomor = createField(5, "|");
                    cetak.append(nomor);
                    StringBuffer tanggalNota = createField(11, "|");
                    cetak.append(tanggalNota);
                    StringBuffer noNota = createField(15, "|");
                    cetak.append(noNota);
                    StringBuffer statusPemabayaran = createField(7, "|");
                    cetak.append(statusPemabayaran);
                    StringBuffer kodeCustomer = createField(10, "|");
                    cetak.append(kodeCustomer);
                    StringBuffer namaCustomer = createField(16, "|");
                    cetak.append(namaCustomer);
                }
//                StringBuffer telpCustomer = createField(14, "|" + itemNota.getNota().getCustomer().getTelp());
//                cetak.append(telpCustomer);
                StringBuffer namaBarang = createField(25, "|" + itemNota.getBarang().getNama());
                cetak.append(namaBarang);
                String hargaBarang = numberFormat.format(itemNota.getHargaJual());
                StringBuffer hargaBarangs = createField(16, "|Rp " + String.valueOf(hargaBarang) + ",00");
                cetak.append(hargaBarangs);
                StringBuffer jumlahBarang = createField(7, "|" + String.valueOf(itemNota.getJumlah()));
                cetak.append(jumlahBarang);
                double subTotal = itemNota.getHargaJual() * itemNota.getJumlah();
                total += subTotal;
                String subTotals = numberFormat.format(subTotal);
                StringBuffer subTota = createField(18, "|Rp " + String.valueOf(subTotals) + ",00");
                cetak.append(subTota + "|\n");
            }
            cetak.append("\n");
            cetak.append("Total : ");
            String totals = numberFormat.format(total);
            StringBuffer totalHarga = createField(13, "Rp " + String.valueOf(totals) + ",00");
            cetak.append(totalHarga);
            pw = new PrintWriter(tujuan);
            pw.write(cetak.toString());
            Runtime.getRuntime().exec(openFile);
        } catch (IOException ex) {
            Logger.getLogger(EngineReport.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            pw.flush();
            pw.close();
        }
    }

    public void printReportPembelian(List listItemPembelian, Date startDate, Date endDate) {
//        PrintWriter pw = null;
        try {
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            StringBuffer cetak = new StringBuffer();
            StringBuffer header = new StringBuffer();
            double total = 0;
            cetak.append("PT BERGUNA JAYA\n");
            cetak.append("Jln. Jembatan Selatan no 470 Panglima Polim\n");
            cetak.append("Telp. 7250923 Fax. 7234053\n");
            cetak.append("Kebayoran Baru - Jakarta Selatan\n");
            cetak.append("                                                           Laporan Pembelian : " + sdf.format(startDate) + " s/d " + sdf.format(endDate) + "\n");
            cetak.append("\n");
            header.append(createField(5, "| NO "));
            header.append(createField(11, "| TANGGAL  "));
            header.append(createField(15, "|     NOTA     "));
            header.append(createField(7, "| T/K  "));
            header.append(createField(10, "| KD.SUPP "));
            header.append(createField(16, "| NAMA SUPPLIER "));
//            header.append(createField(14, "|    TELP     "));
            header.append(createField(25, "|      NAMA BARANG       "));
            header.append(createField(16, "|      HARGA     "));
            header.append(createField(7, "|JUMLAH"));
            header.append(createField(19, "|     SUBTOTAL    |"));
            cetak.append(header + "\n");
            String kode = "";
            int no = 0;
            for (int x = 0; x < listItemPembelian.size(); x++) {
                ItemPembelian itemPembelian = (ItemPembelian) listItemPembelian.get(x);
                if (!itemPembelian.getPembelian().getKode().equals(kode)) {
                    kode = itemPembelian.getPembelian().getKode();
                    StringBuffer nomor = createField(5, "|" + (no += 1));
                    cetak.append(nomor);
                    StringBuffer tanggalNota = createField(11, "|" + sdf.format(itemPembelian.getPembelian().getCreateDate()));
                    cetak.append(tanggalNota);
                    StringBuffer noNota = createField(15, "|" + itemPembelian.getPembelian().getKode());
                    cetak.append(noNota);
                    StringBuffer statusPemabayaran = createField(7, "|" + itemPembelian.getPembelian().getStatusPembayaran());
                    cetak.append(statusPemabayaran);
                    StringBuffer kodeCustomer = createField(10, "|" + itemPembelian.getPembelian().getSupplier().getKode());
                    cetak.append(kodeCustomer);
                    StringBuffer namaCustomer = createField(16, "|" + itemPembelian.getPembelian().getSupplier().getNama());
                    cetak.append(namaCustomer);
                } else {
                    StringBuffer nomor = createField(5, "|");
                    cetak.append(nomor);
                    StringBuffer tanggalNota = createField(11, "|");
                    cetak.append(tanggalNota);
                    StringBuffer noNota = createField(15, "|");
                    cetak.append(noNota);
                    StringBuffer statusPemabayaran = createField(7, "|");
                    cetak.append(statusPemabayaran);
                    StringBuffer kodeCustomer = createField(10, "|");
                    cetak.append(kodeCustomer);
                    StringBuffer namaCustomer = createField(16, "|");
                    cetak.append(namaCustomer);
                }
//                StringBuffer telpCustomer = createField(14, "|" + itemNota.getNota().getCustomer().getTelp());
//                cetak.append(telpCustomer);
                StringBuffer namaBarang = createField(25, "|" + itemPembelian.getBarang().getNama());
                cetak.append(namaBarang);
                String hargaBarang = numberFormat.format(itemPembelian.getHarga());
                StringBuffer hargaBarangs = createField(16, "|Rp " + String.valueOf(hargaBarang) + ",00");
                cetak.append(hargaBarangs);
                StringBuffer jumlahBarang = createField(7, "|" + String.valueOf(itemPembelian.getJumlah()));
                cetak.append(jumlahBarang);
                double subTotal = itemPembelian.getHarga() * itemPembelian.getJumlah();
                total += subTotal;
                String subTotals = numberFormat.format(subTotal);
                StringBuffer subTota = createField(18, "|Rp " + String.valueOf(subTotals) + ",00");
                cetak.append(subTota + "|\n");
            }
            cetak.append("\n");
            cetak.append("Total : ");
            String totals = numberFormat.format(total);
            StringBuffer totalHarga = createField(13, "Rp " + String.valueOf(totals) + ",00");
            cetak.append(totalHarga);
            pw = new PrintWriter(tujuan);
            pw.write(cetak.toString());
            Runtime.getRuntime().exec(openFile);
        } catch (IOException ex) {
            Logger.getLogger(EngineReport.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            pw.flush();
            pw.close();
        }
    }

    public StringBuffer printReportPenjualanTest(List listItemNota, Date startDate, Date endDate) {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        StringBuffer cetak = new StringBuffer();
        StringBuffer header = new StringBuffer();
        double total = 0;
        cetak.append("PT BERGUNA JAYA\n");
        cetak.append("Jln. Jembatan Selatan no 470 Panglima Polim Jakarta Selatan\n");
        cetak.append("Telp : 08174820800\n");
        cetak.append("                                                           Laporan Penjualan : " + sdf.format(startDate) + " s/d " + sdf.format(endDate) + "\n");
        cetak.append("\n");
        header.append(createField(5, "| NO "));
        header.append(createField(11, "| TANGGAL  "));
        header.append(createField(15, "|     NOTA     "));
        header.append(createField(7, "| T/K  "));
        header.append(createField(10, "| KD.CUST "));
        header.append(createField(16, "| NAMA CUSTOMER "));
//            header.append(createField(14, "|    TELP     "));
        header.append(createField(25, "|      NAMA BARANG       "));
        header.append(createField(16, "|      HARGA     "));
        header.append(createField(7, "|JUMLAH"));
        header.append(createField(19, "|     SUBTOTAL    |"));
        cetak.append(header + "\n");
        String kode = "";
        for (int x = 0; x < listItemNota.size(); x++) {
            ItemNota itemNota = (ItemNota) listItemNota.get(x);
            if (!itemNota.getNota().getKode().equals(kode)) {
                kode = itemNota.getNota().getKode();
                StringBuffer nomor = createField(5, "|" + String.valueOf(x + 1));
                cetak.append(nomor);
                StringBuffer tanggalNota = createField(11, "|" + sdf.format(itemNota.getNota().getCreateDate()));
                cetak.append(tanggalNota);
                StringBuffer noNota = createField(15, "|" + itemNota.getNota().getKode());
                cetak.append(noNota);
                StringBuffer statusPemabayaran = createField(7, "|" + itemNota.getNota().getStatusPembayaran());
                cetak.append(statusPemabayaran);
                StringBuffer kodeCustomer = createField(10, "|" + itemNota.getNota().getCustomer().getKode());
                cetak.append(kodeCustomer);
                StringBuffer namaCustomer = createField(16, "|" + itemNota.getNota().getCustomer().getNama());
                cetak.append(namaCustomer);
            } else {
                StringBuffer nomor = createField(5, "|" + String.valueOf(x + 1));
                cetak.append(nomor);
                StringBuffer tanggalNota = createField(11, "|");
                cetak.append(tanggalNota);
                StringBuffer noNota = createField(15, "|");
                cetak.append(noNota);
                StringBuffer statusPemabayaran = createField(7, "|");
                cetak.append(statusPemabayaran);
                StringBuffer kodeCustomer = createField(10, "|");
                cetak.append(kodeCustomer);
                StringBuffer namaCustomer = createField(16, "|");
                cetak.append(namaCustomer);
            }
//                StringBuffer telpCustomer = createField(14, "|" + itemNota.getNota().getCustomer().getTelp());
//                cetak.append(telpCustomer);
            StringBuffer namaBarang = createField(25, "|" + itemNota.getBarang().getNama());
            cetak.append(namaBarang);
            String hargaBarang = numberFormat.format(itemNota.getHargaJual());
            StringBuffer hargaBarangs = createField(16, "|Rp " + String.valueOf(hargaBarang) + ",00");
            cetak.append(hargaBarangs);
            StringBuffer jumlahBarang = createField(7, "|" + String.valueOf(itemNota.getJumlah()));
            cetak.append(jumlahBarang);
            double subTotal = itemNota.getHargaJual() * itemNota.getJumlah();
            total += subTotal;
            String subTotals = numberFormat.format(subTotal);
            StringBuffer subTota = createField(18, "|Rp " + String.valueOf(subTotals) + ",00");
            cetak.append(subTota + "|\n");
        }
        cetak.append("\n");
        cetak.append("Total : ");
        String totals = numberFormat.format(total);
        StringBuffer totalHarga = createField(13, "Rp " + String.valueOf(totals) + ",00");
        cetak.append(totalHarga);
        return cetak;
    }

    public StringBuffer printReportPembelianTest(List listItemPembelian, Date startDate, Date endDate) {

//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        StringBuffer cetak = new StringBuffer();
        StringBuffer header = new StringBuffer();
        double total = 0;
        cetak.append("PT BERGUNA JAYA\n");
        cetak.append("Jln. Jembatan Selatan no 470 Panglima Polim Jakarta Selatan\n");
        cetak.append("Telp : 08174820800\n");
        cetak.append("                                                           Laporan Pembelian : " + sdf.format(startDate) + " s/d " + sdf.format(endDate) + "\n");
        cetak.append("\n");
        header.append(createField(5, "| NO "));
        header.append(createField(11, "| TANGGAL  "));
        header.append(createField(15, "|     NOTA     "));
        header.append(createField(7, "| T/K  "));
        header.append(createField(10, "| KD.SUPP "));
        header.append(createField(16, "| NAMA SUPPLIER "));
//            header.append(createField(14, "|    TELP     "));
        header.append(createField(25, "|      NAMA BARANG       "));
        header.append(createField(16, "|      HARGA     "));
        header.append(createField(7, "|JUMLAH"));
        header.append(createField(19, "|     SUBTOTAL    |"));
        cetak.append(header + "\n");
        String kode = "";
        for (int x = 0; x < listItemPembelian.size(); x++) {
            ItemPembelian itemPembelian = (ItemPembelian) listItemPembelian.get(x);
            if (!itemPembelian.getPembelian().getKode().equals(kode)) {
                kode = itemPembelian.getPembelian().getKode();
                StringBuffer nomor = createField(5, "|" + String.valueOf(x + 1));
                cetak.append(nomor);
                StringBuffer tanggalNota = createField(11, "|" + sdf.format(itemPembelian.getPembelian().getCreateDate()));
                cetak.append(tanggalNota);
                StringBuffer noNota = createField(15, "|" + itemPembelian.getPembelian().getKode());
                cetak.append(noNota);
                StringBuffer statusPemabayaran = createField(7, "|" + itemPembelian.getPembelian().getStatusPembayaran());
                cetak.append(statusPemabayaran);
                StringBuffer kodeCustomer = createField(10, "|" + itemPembelian.getPembelian().getSupplier().getKode());
                cetak.append(kodeCustomer);
                StringBuffer namaCustomer = createField(16, "|" + itemPembelian.getPembelian().getSupplier().getNama());
                cetak.append(namaCustomer);
            } else {
                StringBuffer nomor = createField(5, "|" + String.valueOf(x + 1));
                cetak.append(nomor);
                StringBuffer tanggalNota = createField(11, "|");
                cetak.append(tanggalNota);
                StringBuffer noNota = createField(15, "|");
                cetak.append(noNota);
                StringBuffer statusPemabayaran = createField(7, "|");
                cetak.append(statusPemabayaran);
                StringBuffer kodeCustomer = createField(10, "|");
                cetak.append(kodeCustomer);
                StringBuffer namaCustomer = createField(16, "|");
                cetak.append(namaCustomer);
            }
//                StringBuffer telpCustomer = createField(14, "|" + itemNota.getNota().getCustomer().getTelp());
//                cetak.append(telpCustomer);
            StringBuffer namaBarang = createField(25, "|" + itemPembelian.getBarang().getNama());
            cetak.append(namaBarang);
            String hargaBarang = numberFormat.format(itemPembelian.getHarga());
            StringBuffer hargaBarangs = createField(16, "|Rp " + String.valueOf(hargaBarang) + ",00");
            cetak.append(hargaBarangs);
            StringBuffer jumlahBarang = createField(7, "|" + String.valueOf(itemPembelian.getJumlah()));
            cetak.append(jumlahBarang);
            double subTotal = itemPembelian.getHarga() * itemPembelian.getJumlah();
            total += subTotal;
            String subTotals = numberFormat.format(subTotal);
            StringBuffer subTota = createField(18, "|Rp " + String.valueOf(subTotals) + ",00");
            cetak.append(subTota + "|\n");
        }
        cetak.append("\n");
        cetak.append("Total : ");
        String totals = numberFormat.format(total);
        StringBuffer totalHarga = createField(13, "Rp " + String.valueOf(totals) + ",00");
        cetak.append(totalHarga);
        return cetak;
    }
}

